;
var sizeConvert = (size, level)=>{
	var sizeFix = ["B", "KB", "MB", "GB", "TB"];
	level = typeof(level)=="undefined" ? 0 : level;
	if(size < 1000){
		return size + sizeFix[level];
	}else{
		var a = size/1000;
		if(level < 5){
			return sizeConvert(a, level+1);
		}else{
			return level+1>=5 ? (size + sizeFix[5]) : (a + sizeFix[level+1]);
		}
	}
};

// url 在導航條修改不刷新頁面
function changeUrlWithoutReload(newUrl) {
    history.pushState({ path: newUrl }, "", newUrl);
}

// jq  節點內 表單內容轉成 key=value&..格式 或 FormData
function buildInputs(selecter, flag){
		var formData = {};
		var c_keys = {};
		var files = {};
		var has_file = false;
		selecter.find("input").each(function() {
			var i_name = $(this).attr("name");
		    if($(this).attr("type") == "radio"){
		    	if($(this).is(":checked")){
			    	formData[$(this).attr("name")] = $(this).val();
			    }
	    	}else if($(this).attr("type") == "checkbox"){
	    		if($(this).is(":checked")){
	    			if(i_name.endsWith("[]")){
	    				var i = typeof(c_keys[i_name])!="undefined" ? c_keys[i_name] : -1;
	    				i = i + 1;
	    				new_name = i_name.slice(0, -2)+"["+i+"]";
	    				formData[new_name] = $(this).val();
	    				c_keys[i_name] = i;
    				}else{
    					formData[$(this).attr("name")] = $(this).val();
    				}
	    		}
	    	}else{
	    		if($(this).attr("type") == "file"){
	    			has_file = true;
	    			formData[$(this).attr("name")] = $(this).val();
	    			files[i_name] = $("[name=\'"+i_name+"\']")[0].files[0];
	    		}else{
	    			formData[$(this).attr("name")] = $(this).val();
	    		}
	    	}
		    
		});
		selecter.find("select").each(function() {
		    formData[$(this).attr("name")] = $(this).val();
		});
		if(flag||has_file){
			var data = new FormData;
			for(var x in formData){
				if(typeof(files[x])!="undefined"){
					data.append(x, files[x]);
				}else{
					data.append(x, formData[x]);
				}
			}
			return data;
		}else{
			var data = "";
			for(var x in formData){
				data += x+"="+encodeURIComponent(formData[x])+"&";
			}
			return data.slice(0, -1);
		}
	}
// 獲取瀏覽器類型
function detectEnvironment() {
    const userAgent = navigator.userAgent || navigator.vendor || window.opera;
 
    if (/android/i.test(userAgent)) {
        return "Android Webview";
    } else if (/(iphone|ipad|ipod)/i.test(userAgent)) {
        return "iOS Webview";
    } else if (/chrome/i.test(userAgent)) {
        return "Chrome Browser";
    } else if (/firefox/i.test(userAgent)) {
        return "Firefox Browser";
    } else if (/safari/i.test(userAgent)) {
        return "Safari Browser";
    } else {
    	if (window.cordova) {
	        return "Cordova Webview";
	    } else if (window.hasOwnProperty('WebViewJavascriptBridge')) {
	        return "WebviewJavascriptBridge";
	    } else {
	        return "Browser";
	    }
        return "Unknown Environment";
    }
}
CookieDo = {
	get:function(cname) {
	    var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++) 
		{
		    var c = ca[i].trim();
		    if (c.indexOf(name)==0) return c.substring(name.length,c.length);
		}
		return "";
	}
	,set:function(c_name, value, expire_days){
		let exDate = new Date();
	    exDate.setDate(exDate.getDate() + expire_days);
	    document.cookie =
	        c_name +
	        '=' +
	        encodeURIComponent(value) +
	        ';expires=' +
	        exDate.toUTCString() +
	        ';path=/';
	}
	,delete:function(name){
		this.set(name, '', -1);
	}
};

class DateM{
	constructor(date){
		this.date = date||new Date();
	}
	getNextMonth(){
		var r = new Date(this.date);
		r.setMonth(this.date.getMonth() + 1);
		return r;
	}
	getPrevMonth(){
		var r = new Date(this.date);
		r.setMonth(this.date.getMonth() - 1);
		return r;
	}
	getPrevDate(){
		var r = new Date(this.date);
		r.setDate(this.date.getDate() - 1);
		return r;
	}
	getNextDate(){
		var r = new Date(this.date);
		r.setDate(this.date.getDate() + 1);
		return r;
	}
	getPrevWeek(){
		var r = new Date(this.date);
		var dayOfWeek = this.date.getDay();	
		dayOfWeek = ((1 - dayOfWeek + 7) % 7);
		dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
		r.setDate(this.date.getDate() - dayOfWeek);
		return r;
	}
	getNextWeek(){
		var r = new Date(this.date);
		var dayOfWeek = this.date.getDay();		
		dayOfWeek = ((1 - dayOfWeek + 7) % 7);
		dayOfWeek = dayOfWeek == 0 ? 7 : dayOfWeek;
		r.setDate(this.date.getDate() + dayOfWeek);
		return r;
	}
}

$(function () {
	
	$(document).on("click", "[data-filter='upload']", async function(){
		var target = $(this).data("target");
		$("#"+target).trigger("click");
	});
	$(document).on("change", "input[type='file']", function(){
		var file = this.files[0]; // 获取选中的文件
        var target = $(this).data("target");
        if (file) {
            var fileName = file.name; // 文件名
            var fileSize = sizeConvert(file.size); // 文件大小（字节）
            $("#"+target).val(file.name + "/"+fileSize);
            $("#"+target).show();
        }else{
        	$("#"+target).hide();
        }
	});
});

//瀏覽器自帶分享頁面
$(()=>{
	var shareData= {
		"title":"'.$this->title.'",
		"url":"'.Url::current().'"
	};
	$(document).on("click", "[data-on=\'shares\']", async function(){
		try {
		    await navigator.share(shareData);
		} catch (err) {
		  	console.log(err);
		}
	});
});